﻿import cgi
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from entities import *
#from entities import NhaTuyenDungEntities
#from entities import DangTuyenDoiTuongEntities
#from entities import TuyenDungNguoiDungEntities

from Entities import *
#from entities import NganhNgheEntities
#from entities import TrangThaiEntities
from datetime import date
import datetime
#---------------------------
# Entities DonDangTuyen
#---------------------------
def Get(keyNguoiDung, NhaTuyenDungID=0 , DonDangTuyenID=0, txtSearch = "", NgayDg="", NgayKT="", TinhThanhID = 0, CapBacID = 0, TrangThaiID=0, MucLuongID = 0, LoaiHinhID=0, LinhVucID = 0, NganhNgheID=0, DoiTuongID = 0):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    dondangtuyen_list = []
    results = DonDangTuyen.all()
    if keyNguoiDung != Entities.GetKey():
        ddt = GetDt_NhaTD(keyNguoiDung)
        results = results.filter("__key__ IN ", ddt)
    #try: 

    if txtSearch != "":
        results = results.filter("ChucDanh >=", txtSearch).filter("ChucDanh <=", unicode(txtSearch) + u"\ufffd") 
    else:
        if NgayDg != "" :
            NgayDang = datetime.datetime.strptime(NgayDg,"%d/%m/%Y")
            results = results.filter("NgayDang >=",NgayDang)
        if NgayKT != "" :
            NgayKetThuc = datetime.datetime.strptime(NgayKT,"%d/%m/%Y" )
            results = results.filter("NgayDang <=",NgayKetThuc)    
    if NhaTuyenDungID > 0:
        nhatuyendung = NhaTuyenDung.get_by_id(NhaTuyenDungID)
        #if nhatuyendung != None:
        results = results.filter("NhaTuyenDungID", nhatuyendung)
    if TrangThaiID > 0:
        trangthai = TrangThaiXetDuyet.get_by_id(TrangThaiID)
        #if trangthai != None:
        results = results.filter("TrangThaiID", trangthai)    
    if MucLuongID > 0:
        mucluong = MucLuong.get_by_id(MucLuongID)
        #if mucluong != None:
        results = results.filter("MucLuongID", mucluong)              
            
    if NganhNgheID > 0:
        nganhnghe = NganhNghe.get_by_id(NganhNgheID)
        #if nganhnghe != None:
        results = results.filter("NganhNgheID", nganhnghe)  
    if CapBacID > 0:
        capbac = CapBac.get_by_id(CapBacID)
        #if capbac != None:
        results = results.filter("CapBacID", capbac)     
    if LoaiHinhID > 0:
        loaihinh = LoaiHinhCongViec.get_by_id(LoaiHinhID)
        #if loaihinh != None:
        results = results.filter("LoaiHinhID", loaihinh) 
    if LinhVucID > 0:
        tuple_NganhNghe = NganhNgheEntities.Get(keyNguoiDung, LinhVucID)
        #return list_NganhNghe
        nnLinhVucList = []
        for keyNganhNghe in tuple_NganhNghe:
            nnID = keyNganhNghe["NganhNgheID"]
            nnLinhVuc  = Entities.NganhNghe.get_by_id(nnID)
            nnLinhVucList.append(nnLinhVuc)
        results = results.filter("NganhNgheID IN ", nnLinhVucList)
    if TinhThanhID > 0:
        tinhthanh = TinhThanh.get_by_id(TinhThanhID)
        #if tinhthanh != None:
        results = results.filter("TinhThanhID", tinhthanh)
    if DoiTuongID > 0:
        doituong = DangTuyenDoiTuongEntities.GetIDDangTuyen(keyNguoiDung, DoiTuongID)
        results = results.filter("__key__ IN", doituong)
    if DonDangTuyenID > 0:
        dondangtuyen = DonDangTuyen.get_by_id(DonDangTuyenID)
        dondangtuyen_dict ={}
        dondangtuyen_dict['DonDangTuyenID'] = dondangtuyen.key().id()
        dondangtuyen_dict['NhaTuyenDungID'] = dondangtuyen.NhaTuyenDungID.key().id()
        dondangtuyen_dict['NhaTuyenDung'] = dondangtuyen.NhaTuyenDungID.TenThuongHieu
        dondangtuyen_dict['NgayDang'] = dondangtuyen.NgayDang.strftime("%d/%m/%Y")
        dondangtuyen_dict['NgayKetThuc'] = dondangtuyen.NgayKetThuc.strftime("%d/%m/%Y")
        dondangtuyen_dict['MieuTa'] = dondangtuyen.MieuTa
        dondangtuyen_dict['YeuCau'] = dondangtuyen.YeuCau
        dondangtuyen_dict['NganhNghe'] = dondangtuyen.NganhNgheID.TenNganhNghe
        dondangtuyen_dict['NganhNgheID'] = dondangtuyen.NganhNgheID.key().id()
        dondangtuyen_dict['TrangThai'] = dondangtuyen.TrangThaiID.TenTrangThai
        dondangtuyen_dict['ChucDanh'] = dondangtuyen.ChucDanh
        dondangtuyen_dict['MucLuong'] = dondangtuyen.MucLuongID.TenMucLuong
        dondangtuyen_dict['MucLuongID'] = dondangtuyen.MucLuongID.key().id()
        dondangtuyen_dict['LoaiHinh'] = dondangtuyen.LoaiHinhID.TenLoaiHinh    
        dondangtuyen_dict['LoaiHinhID'] = dondangtuyen.LoaiHinhID.key().id()  
        dondangtuyen_dict['CapBac'] = dondangtuyen.CapBacID.TenCapBac
        dondangtuyen_dict['CapBacID'] = dondangtuyen.CapBacID.key().id()
        dondangtuyen_dict['TinhThanh'] = dondangtuyen.TinhThanhID.TenTinhThanh
        dondangtuyen_dict['TinhThanhID'] = dondangtuyen.TinhThanhID.key().id()
        dondangtuyen_list.append(dondangtuyen_dict)
    else:
        if(txtSearch ==""):
            results = results.order("-NgayDang")
        for dondangtuyen in results:
            dondangtuyen_dict ={}
            dondangtuyen_dict['DonDangTuyenID'] = dondangtuyen.key().id()
            dondangtuyen_dict['NhaTuyenDungID'] = dondangtuyen.NhaTuyenDungID.key().id()
            dondangtuyen_dict['NhaTuyenDung'] = dondangtuyen.NhaTuyenDungID.TenThuongHieu
            dondangtuyen_dict['NgayDang'] = dondangtuyen.NgayDang.strftime("%d/%m/%Y")
            dondangtuyen_dict['NgayKetThuc'] = dondangtuyen.NgayKetThuc.strftime("%d/%m/%Y")
            dondangtuyen_dict['MieuTa'] = dondangtuyen.MieuTa
            dondangtuyen_dict['YeuCau'] = dondangtuyen.YeuCau
            dondangtuyen_dict['NganhNghe'] = dondangtuyen.NganhNgheID.TenNganhNghe
            dondangtuyen_dict['NganhNgheID'] = dondangtuyen.NganhNgheID.key().id()
            dondangtuyen_dict['TrangThai'] = dondangtuyen.TrangThaiID.TenTrangThai
            dondangtuyen_dict['ChucDanh'] = dondangtuyen.ChucDanh
            dondangtuyen_dict['MucLuong'] = dondangtuyen.MucLuongID.TenMucLuong
            dondangtuyen_dict['MucLuongID'] = dondangtuyen.MucLuongID.key().id()
            dondangtuyen_dict['LoaiHinh'] = dondangtuyen.LoaiHinhID.TenLoaiHinh
            dondangtuyen_dict['LoaiHinhID'] = dondangtuyen.LoaiHinhID.key().id() 
            dondangtuyen_dict['CapBac'] = dondangtuyen.CapBacID.TenCapBac
            dondangtuyen_dict['CapBacID'] = dondangtuyen.CapBacID.key().id()
            dondangtuyen_dict['TinhThanh'] = dondangtuyen.TinhThanhID.TenTinhThanh
            dondangtuyen_dict['TinhThanhID'] = dondangtuyen.TinhThanhID.key().id()
            dondangtuyen_list.append(dondangtuyen_dict)
    tuple_dondangtuyen = tuple(dondangtuyen_list)  
    return tuple_dondangtuyen
    #except:
        #return "Error"
def GetService(keyNguoiDung, NhaTuyenDungID=0 , DonDangTuyenID=0, txtSearch = "", NgayDg="", NgayKT="", TinhThanhID = 0, CapBacID = 0, TrangThaiID=0, MucLuongID = 0, LoaiHinhID=0, LinhVucID = 0, NganhNgheID=0, DoiTuongID = 0):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    dondangtuyen_list = []
    results = DonDangTuyen.all()
    if keyNguoiDung != Entities.GetKey():
        ddt = GetDt_NhaTD(keyNguoiDung)
        results = results.filter("__key__ IN ", ddt)
    #try: 

    if txtSearch != "":
        results = results.filter("ChucDanh >=", txtSearch).filter("ChucDanh <=", unicode(txtSearch) + u"\ufffd") 
    else:
        if NgayDg != "" :
            NgayDang = datetime.datetime.strptime(NgayDg,"%d/%m/%Y")
            results = results.filter("NgayDang >=",NgayDang)
        if NgayKT != "" :
            NgayKetThuc = datetime.datetime.strptime(NgayKT,"%d/%m/%Y" )
            results = results.filter("NgayDang <=",NgayKetThuc)    
    if NhaTuyenDungID > 0:
        nhatuyendung = NhaTuyenDung.get_by_id(NhaTuyenDungID)
        #if nhatuyendung != None:
        results = results.filter("NhaTuyenDungID", nhatuyendung)
    if TrangThaiID > 0:
        trangthai = TrangThaiXetDuyet.get_by_id(TrangThaiID)
        #if trangthai != None:
        results = results.filter("TrangThaiID", trangthai)    
    if MucLuongID > 0:
        mucluong = MucLuong.get_by_id(MucLuongID)
        #if mucluong != None:
        results = results.filter("MucLuongID", mucluong)              
            
    if NganhNgheID > 0:
        nganhnghe = NganhNghe.get_by_id(NganhNgheID)
        #if nganhnghe != None:
        results = results.filter("NganhNgheID", nganhnghe)  
    if CapBacID > 0:
        capbac = CapBac.get_by_id(CapBacID)
        #if capbac != None:
        results = results.filter("CapBacID", capbac)     
    if LoaiHinhID > 0:
        loaihinh = LoaiHinhCongViec.get_by_id(LoaiHinhID)
        #if loaihinh != None:
        results = results.filter("LoaiHinhID", loaihinh) 
    if LinhVucID > 0:
        tuple_NganhNghe = NganhNgheEntities.Get(keyNguoiDung, LinhVucID)
        #return list_NganhNghe
        nnLinhVucList = []
        for keyNganhNghe in tuple_NganhNghe:
            nnID = keyNganhNghe["NganhNgheID"]
            nnLinhVuc  = Entities.NganhNghe.get_by_id(nnID)
            nnLinhVucList.append(nnLinhVuc)
        results = results.filter("NganhNgheID IN ", nnLinhVucList)
    if TinhThanhID > 0:
        tinhthanh = TinhThanh.get_by_id(TinhThanhID)
        #if tinhthanh != None:
        results = results.filter("TinhThanhID", tinhthanh)
    if DoiTuongID > 0:
        doituong = DangTuyenDoiTuongEntities.GetIDDangTuyen(keyNguoiDung, DoiTuongID)
        results = results.filter("__key__ IN", doituong)
    if DonDangTuyenID > 0:
        dondangtuyen = DonDangTuyen.get_by_id(DonDangTuyenID)
        dondangtuyen_dict ={}
        dondangtuyen_dict['DonDangTuyenID'] = dondangtuyen.key().id()
        dondangtuyen_dict['NhaTuyenDungID'] = dondangtuyen.NhaTuyenDungID.key().id()
        dondangtuyen_dict['NhaTuyenDung'] = dondangtuyen.NhaTuyenDungID.TenThuongHieu
        dondangtuyen_dict['NgayDang'] = dondangtuyen.NgayDang
        dondangtuyen_dict['NgayKetThuc'] = dondangtuyen.NgayKetThuc
        dondangtuyen_dict['MieuTa'] = dondangtuyen.MieuTa
        dondangtuyen_dict['YeuCau'] = dondangtuyen.YeuCau
        dondangtuyen_dict['NganhNghe'] = dondangtuyen.NganhNgheID.TenNganhNghe
        dondangtuyen_dict['NganhNgheID'] = dondangtuyen.NganhNgheID.key().id()
        dondangtuyen_dict['TrangThai'] = dondangtuyen.TrangThaiID.TenTrangThai
        dondangtuyen_dict['ChucDanh'] = dondangtuyen.ChucDanh
        dondangtuyen_dict['MucLuong'] = dondangtuyen.MucLuongID.TenMucLuong
        dondangtuyen_dict['MucLuongID'] = dondangtuyen.MucLuongID.key().id()
        dondangtuyen_dict['LoaiHinh'] = dondangtuyen.LoaiHinhID.TenLoaiHinh    
        dondangtuyen_dict['LoaiHinhID'] = dondangtuyen.LoaiHinhID.key().id()  
        dondangtuyen_dict['CapBac'] = dondangtuyen.CapBacID.TenCapBac
        dondangtuyen_dict['CapBacID'] = dondangtuyen.CapBacID.key().id()
        dondangtuyen_dict['TinhThanh'] = dondangtuyen.TinhThanhID.TenTinhThanh
        dondangtuyen_dict['TinhThanhID'] = dondangtuyen.TinhThanhID.key().id()
        dondangtuyen_list.append(dondangtuyen_dict)
    else:
        if(txtSearch ==""):
            results = results.order("-NgayDang")
        for dondangtuyen in results:
            dondangtuyen_dict ={}
            dondangtuyen_dict['DonDangTuyenID'] = dondangtuyen.key().id()
            dondangtuyen_dict['NhaTuyenDungID'] = dondangtuyen.NhaTuyenDungID.key().id()
            dondangtuyen_dict['NhaTuyenDung'] = dondangtuyen.NhaTuyenDungID.TenThuongHieu
            dondangtuyen_dict['NgayDang'] = dondangtuyen.NgayDang
            dondangtuyen_dict['NgayKetThuc'] = dondangtuyen.NgayKetThuc
            dondangtuyen_dict['MieuTa'] = dondangtuyen.MieuTa
            dondangtuyen_dict['YeuCau'] = dondangtuyen.YeuCau
            dondangtuyen_dict['NganhNghe'] = dondangtuyen.NganhNgheID.TenNganhNghe
            dondangtuyen_dict['NganhNgheID'] = dondangtuyen.NganhNgheID.key().id()
            dondangtuyen_dict['TrangThai'] = dondangtuyen.TrangThaiID.TenTrangThai
            dondangtuyen_dict['ChucDanh'] = dondangtuyen.ChucDanh
            dondangtuyen_dict['MucLuong'] = dondangtuyen.MucLuongID.TenMucLuong
            dondangtuyen_dict['MucLuongID'] = dondangtuyen.MucLuongID.key().id()
            dondangtuyen_dict['LoaiHinh'] = dondangtuyen.LoaiHinhID.TenLoaiHinh
            dondangtuyen_dict['LoaiHinhID'] = dondangtuyen.LoaiHinhID.key().id() 
            dondangtuyen_dict['CapBac'] = dondangtuyen.CapBacID.TenCapBac
            dondangtuyen_dict['CapBacID'] = dondangtuyen.CapBacID.key().id()
            dondangtuyen_dict['TinhThanh'] = dondangtuyen.TinhThanhID.TenTinhThanh
            dondangtuyen_dict['TinhThanhID'] = dondangtuyen.TinhThanhID.key().id()
            dondangtuyen_list.append(dondangtuyen_dict)
    tuple_dondangtuyen = tuple(dondangtuyen_list)  
    return tuple_dondangtuyen

        
def GetDt_NhaTD(keyNguoiDung):
    ntd_list = []
    results = NhaTuyenDung.all()
    tuyendungs = TuyenDungNguoiDungEntities.Get(keyNguoiDung,0)    
    results = results.filter("__key__ IN", tuyendungs)
    for nhatuyendung in results:
        ntd_list.append(nhatuyendung.key()) 
    ddt_list = []
    for ntd in ntd_list:
        nhatuyendung = NhaTuyenDung.get_by_id(ntd.id())
        results = DonDangTuyen.all().filter("NhaTuyenDungID", nhatuyendung)
        for dt in results:
            ddt_list.append(dt.key())
    return ddt_list   

def Add(keyNguoiDung, NgayDang, NgayKT, MieuTa, YeuCau, NhaTuyenDungID, NganhNgheID, ChucDanh,MucLuongID, LoaiHinhID, TinhThanhID, CapBacID, TrangThaiID, DoiTuongs):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    nhatuyendung = NhaTuyenDung.get_by_id(int(NhaTuyenDungID))
    nganhnghe = NganhNghe.get_by_id(int(NganhNgheID))
    tinhthanh = TinhThanh.get_by_id(int(TinhThanhID))
    mucluong = MucLuong.get_by_id(int(MucLuongID))
    loaihinh = LoaiHinhCongViec.get_by_id(int(LoaiHinhID))
    trangthai =TrangThaiXetDuyet.get_by_id(int(TrangThaiID))
    capbac = CapBac.get_by_id(int(CapBacID))
    NgayKetThuc = datetime.datetime.strptime(NgayKT,"%d/%m/%Y" )
    dondangtuyen = db.GqlQuery("SELECT * FROM DonDangTuyen WHERE  NgayKetThuc = :1 AND MieuTa =:2 AND YeuCau = :3 AND NhaTuyenDungID = :4 AND ChucDanh= :5 ",
                              NgayKetThuc, MieuTa, YeuCau, nhatuyendung, ChucDanh)

    if nganhnghe != None and tinhthanh!= None and nhatuyendung!= None  and trangthai!= None and mucluong!= None and loaihinh != None:
        if dondangtuyen.count() == 0:                      
            dondangtuyen = DonDangTuyen(NgayDang = NgayDang, NgayKetThuc= NgayKetThuc, MieuTa = MieuTa, YeuCau = YeuCau,
                                        NhaTuyenDungID = nhatuyendung, NganhNgheID = nganhnghe, TrangThaiID = trangthai,
										MucLuongID = mucluong, LoaiHinhID = loaihinh,
                                        ChucDanh = ChucDanh, TinhThanhID = tinhthanh, CapBacID = capbac)
            dondangtuyen.put()
            DonDangTuyenID = dondangtuyen.key().id()
            for dt in DoiTuongs:
                 DangTuyenDoiTuongEntities.Add(keyNguoiDung, DonDangTuyenID, dt)	
            return DonDangTuyenID
        else:
            return "Duplicate"

def AddWithOutDoiTuong(keyNguoiDung, NgayDang, NgayKT, MieuTa, YeuCau, NhaTuyenDungID, NganhNgheID, ChucDanh,MucLuongID, LoaiHinhID, TinhThanhID, CapBacID, TrangThaiID):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    nhatuyendung = NhaTuyenDung.get_by_id(int(NhaTuyenDungID))
    nganhnghe = NganhNghe.get_by_id(int(NganhNgheID))
    tinhthanh = TinhThanh.get_by_id(int(TinhThanhID))
    mucluong = MucLuong.get_by_id(int(MucLuongID))
    loaihinh = LoaiHinhCongViec.get_by_id(int(LoaiHinhID))
    trangthai =TrangThaiXetDuyet.get_by_id(int(TrangThaiID))
    capbac = CapBac.get_by_id(int(CapBacID))
    NgayKetThuc = datetime.datetime.strptime(NgayKT,"%d/%m/%Y" )
    dondangtuyen = db.GqlQuery("SELECT * FROM DonDangTuyen WHERE NgayDang = :1 AND NgayKetThuc = :2 AND MieuTa =:3 AND YeuCau = :4 AND NhaTuyenDungID = :5 AND ChucDanh= :6 AND NguoiDung =:7",
                              NgayDang, NgayKetThuc, MieuTa, YeuCau, nhatuyendung, ChucDanh, nguoidung)

    if nganhnghe != None and tinhthanh!= None and nhatuyendung!= None  and trangthai!= None and mucluong!= None and loaihinh != None:
        if dondangtuyen.count() == 0:                      
            dondangtuyen = DonDangTuyen(NgayDang = NgayDang, NgayKetThuc= NgayKetThuc, MieuTa = MieuTa, YeuCau = YeuCau,
                                        NhaTuyenDungID = nhatuyendung, NganhNgheID = nganhnghe, TrangThaiID = trangthai,
										MucLuongID = mucluong, LoaiHinhID = loaihinh,
                                        ChucDanh = ChucDanh, TinhThanhID = tinhthanh, CapBacID = capbac)
            dondangtuyen.put()
            DonDangTuyenID = dondangtuyen.key().id()
            return str(DonDangTuyenID)
        else:
            return "Duplicate"   
            
def Update(keyNguoiDung, DonDangTuyenID, NgayKT, MieuTa, YeuCau, NganhNgheID, ChucDanh, MucLuongID, LoaiHinhID, TinhThanhID, CapBacID):
        try:
            nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
        except:
            return "Invalid Key"
        dondangtuyen_update = DonDangTuyen.get_by_id(DonDangTuyenID)
        results = DonDangTuyen.all()
        if dondangtuyen_update != None:
            #dondangtuyen = db.GqlQuery("SELECT * FROM DonDangTuyen WHERE NgayDang = :1 AND NgayKetThuc = :2 AND MieuTa =:3 AND YeuCau = :4 AND NhaTuyenDungID = :5" +
            #                   "AND NganhNgheID= :6 AND ChucDanh= : 7 AND TinhThanhID= :8 AND NguoiDungID =:9 AND NguoiDung =:10",
            #                  NgayDang, NgayKetThuc, MieuTa, YeuCau, nhatuyendung, nganhnghe, ChucDanh, tinhthanh, NguoiDungID, nguoidung)          
            if int(NganhNgheID) > 0: 
                nganhnghe = NganhNghe.get_by_id(NganhNgheID)
                dondangtuyen_update.NganhNgheID = nganhnghe
                results = results.filter("NganhNgheID", nganhnghe)
            if int(TinhThanhID) > 0:
                tinhthanh = TinhThanh.get_by_id(TinhThanhID)
                dondangtuyen_update.TinhThanhID = tinhthanh
                results = results.filter("TinhThanhID", tinhthanh)
            #if NgayDang != "":
            #    dondangtuyen_update.NgayDang = datetime.datetime.now()
            #    results = results.filter("NgayDang=", NgayDang)
            if NgayKT != "":
                NgayKetThuc = datetime.datetime.strptime(NgayKT,"%d/%m/%Y" )
                dondangtuyen_update.NgayKetThuc = NgayKetThuc
                
            if MieuTa != "":
                dondangtuyen_update.MieuTa = MieuTa
                results = results.filter("MieuTa = ", MieuTa)
            if YeuCau != "":
                dondangtuyen_update.YeuCau = YeuCau
                results = results.filter("YeuCau = ", YeuCau)
            if ChucDanh != "":    
                dondangtuyen_update.ChucDanh = ChucDanh
                results = results.filter("ChucDanh = ", ChucDanh)
            if CapBacID > 0: 
                capbac = CapBac.get_by_id(CapBacID)
                dondangtuyen_update.CapBacID = capbac
                results = results.filter("CapBacID", capbac)
            if MucLuongID > 0: 
                mucluong = MucLuong.get_by_id(MucLuongID)
                dondangtuyen_update.MucLuongID = mucluong
                results = results.filter("MucLuongID", mucluong)                
            if results.count() == 0:    
                dondangtuyen_update.put()
                return "Complete"
            else:
                return "Duplicate"
        else:
            return "Error"

def Delete(keyNguoiDung,DonDangTuyenID = 0,NhaTuyenDungID=0, NganhNgheID=0):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"    
    results = DonDangTuyen.all()
    if int(NhaTuyenDungID) > 0:
        nhatuyendung = NhaTuyenDung.get_by_id(int(NhaTuyenDungID))
        results = results.filter("NhaTuyenDungID", nhatuyendung)
    if int(NganhNgheID) > 0:
        nganhnghe = NganhNghe.get_by_id(int(NganhNgheID))
        results = results.filter("NganhNgheID", nganhnghe)
    if DonDangTuyenID > 0:
        dondangtuyen = DonDangTuyen.get_by_id(DonDangTuyenID)
        DangTuyenDoiTuongEntities.Delete(keyNguoiDung,DonDangTuyenID)
        DonDangTuyen.delete(dondangtuyen)
    else :
        for dondangtuyen in results:
            DangTuyenDoiTuongEntities.Delete(keyNguoiDung,dondangtuyen.key().id())
            DonDangTuyen.delete(dondangtuyen)
            
    return "Complete"

        
def All(NhaTuyenDungID=0 , DonDangTuyenID=0, txtSearch = "", NgayDg="", NgayKT="", TinhThanhID = 0, CapBacID = 0, TrangThaiID=0, MucLuongID = 0, LoaiHinhID=0, LinhVucID = 0, NganhNgheID=0, DoiTuongID = 0):
    dondangtuyen_list = []
    results = DonDangTuyen.all()
    if txtSearch != "":
        results = results.filter("ChucDanh >=", txtSearch).filter("ChucDanh <=", unicode(txtSearch) + u"\ufffd") 
    else:
        if NgayDg != "" :
            NgayDang = datetime.datetime.strptime(NgayDg,"%d/%m/%Y")
            results = results.filter("NgayDang >=",NgayDang)
        if NgayKT != "" :
            NgayKetThuc = datetime.datetime.strptime(NgayKT,"%d/%m/%Y" )
            results = results.filter("NgayDang <=",NgayKetThuc)       
    if NhaTuyenDungID > 0:
        nhatuyendung = NhaTuyenDung.get_by_id(NhaTuyenDungID)
        results = results.filter("NhaTuyenDungID", nhatuyendung)
    if TrangThaiID > 0:
        trangthai = TrangThaiXetDuyet.get_by_id(TrangThaiID)
        results = results.filter("TrangThaiID", trangthai)    
    if MucLuongID > 0:
        mucluong = MucLuong.get_by_id(MucLuongID)
        results = results.filter("MucLuongID", mucluong)              
    if NganhNgheID > 0:
        nganhnghe = Entities.NganhNghe.get_by_id(NganhNgheID)
        results = results.filter("NganhNgheID", nganhnghe)  
    if CapBacID > 0:
        capbac = CapBac.get_by_id(CapBacID)
        results = results.filter("CapBacID", capbac)     
    if LoaiHinhID > 0:
        loaihinh = LoaiHinhCongViec.get_by_id(LoaiHinhID)
        results = results.filter("LoaiHinhID", loaihinh) 
    if LinhVucID > 0:
        tuple_NganhNghe = NganhNghe.Get(Entities.GetKey(), LinhVucID)
        nnLinhVucList = []
        for keyNganhNghe in tuple_NganhNghe:
            nnID = keyNganhNghe["NganhNgheID"]
            nnLinhVuc  = Entities.NganhNghe.get_by_id(nnID)
            nnLinhVucList.append(nnLinhVuc)
        results = results.filter("NganhNgheID IN ", nnLinhVucList)
    if TinhThanhID > 0:
        tinhthanh = TinhThanh.get_by_id(TinhThanhID)
        results = results.filter("TinhThanhID", tinhthanh)
    if DoiTuongID > 0:
        doituong = DangTuyenDoiTuongEntities.GetIDDangTuyen(Entities.GetKey(), DoiTuongID)
        results = results.filter("__key__ IN", doituong)  
    if DonDangTuyenID > 0:
        dondangtuyen = DonDangTuyen.get_by_id(DonDangTuyenID)
        dondangtuyen_dict ={}
        dondangtuyen_dict['DonDangTuyenID'] = dondangtuyen.key().id()
        dondangtuyen_dict['NhaTuyenDungID'] = dondangtuyen.NhaTuyenDungID.key().id()
        dondangtuyen_dict['NhaTuyenDung'] = dondangtuyen.NhaTuyenDungID.TenThuongHieu
        dondangtuyen_dict['NgayDang'] = dondangtuyen.NgayDang.strftime("%d/%m/%Y")
        dondangtuyen_dict['NgayKetThuc'] = dondangtuyen.NgayKetThuc.strftime("%d/%m/%Y")
        dondangtuyen_dict['MieuTa'] = dondangtuyen.MieuTa
        dondangtuyen_dict['YeuCau'] = dondangtuyen.YeuCau
        dondangtuyen_dict['NganhNghe'] = dondangtuyen.NganhNgheID.TenNganhNghe
        dondangtuyen_dict['TrangThai'] = dondangtuyen.TrangThaiID.TenTrangThai
        dondangtuyen_dict['ChucDanh'] = dondangtuyen.ChucDanh
        dondangtuyen_dict['MucLuong'] = dondangtuyen.MucLuongID.TenMucLuong
        dondangtuyen_dict['LoaiHinh'] = dondangtuyen.LoaiHinhID.TenLoaiHinh    
        dondangtuyen_dict['CapBac'] = dondangtuyen.CapBacID.TenCapBac
        dondangtuyen_dict['TinhThanh'] = dondangtuyen.TinhThanhID.TenTinhThanh
        dondangtuyen_list.append(dondangtuyen_dict)
    else:
        if(txtSearch ==""):
            results = results.order("-NgayDang")
        for dondangtuyen in results:
            dondangtuyen_dict ={}
            dondangtuyen_dict['DonDangTuyenID'] = dondangtuyen.key().id()
            dondangtuyen_dict['NhaTuyenDungID'] = dondangtuyen.NhaTuyenDungID.key().id()
            dondangtuyen_dict['NhaTuyenDung'] = dondangtuyen.NhaTuyenDungID.TenThuongHieu
            dondangtuyen_dict['NgayDang'] = dondangtuyen.NgayDang.strftime("%d/%m/%Y")
            dondangtuyen_dict['NgayKetThuc'] = dondangtuyen.NgayKetThuc.strftime("%d/%m/%Y")
            dondangtuyen_dict['MieuTa'] = dondangtuyen.MieuTa
            dondangtuyen_dict['YeuCau'] = dondangtuyen.YeuCau
            dondangtuyen_dict['NganhNghe'] = dondangtuyen.NganhNgheID.TenNganhNghe
            dondangtuyen_dict['TrangThai'] = dondangtuyen.TrangThaiID.TenTrangThai
            dondangtuyen_dict['ChucDanh'] = dondangtuyen.ChucDanh
            dondangtuyen_dict['MucLuong'] = dondangtuyen.MucLuongID.TenMucLuong
            dondangtuyen_dict['LoaiHinh'] = dondangtuyen.LoaiHinhID.TenLoaiHinh    
            dondangtuyen_dict['CapBac'] = dondangtuyen.CapBacID.TenCapBac
            dondangtuyen_dict['TinhThanh'] = dondangtuyen.TinhThanhID.TenTinhThanh
            dondangtuyen_dict['NhaTuyenDungImg'] = '/img?img_id=' + str(dondangtuyen.NhaTuyenDungID.key())
            dondangtuyen_list.append(dondangtuyen_dict)
    tuple_dondangtuyen = tuple(dondangtuyen_list)  
    return tuple_dondangtuyen




    
def UpdateStatus(DonDangTuyenID,TrangThaiID):
    trangthai = TrangThaiXetDuyet.get_by_id(TrangThaiID)
    dondangtuyen = DonDangTuyen.get_by_id(DonDangTuyenID)
    dondangtuyen.TrangThaiID = trangthai
    dondangtuyen.put()
    return "Complete"
    
def UpdateTrangThai(keyNguoiDung,DonDangTuyenID, TenTrangThai):
    #results = TrangThaiXetDuyet.all().filter("TenTrangThai=", TenTrangThai)
    #return results
    TrangThaiID = TrangThai.GetID(keyNguoiDung, unicode(TenTrangThai))
    dondangtuyen = DonDangTuyen.get_by_id(DonDangTuyenID)
    if TrangThaiID > 0:
        trangthai = TrangThaiXetDuyet.get_by_id(TrangThaiID)
        dondangtuyen.TrangThaiID = trangthai
        dondangtuyen.put()
        return "Complete"
    else:
        return "Error"